import java.util.*;

public class Solution {
    static class Envelop implements Comparable {
        public Integer w, h;

        public Envelop(int w, int h) {
            this.w = w;
            this.h = h;
        }

        @Override
        public int compareTo(Object o) {
            Envelop t = (Envelop) o;
            if (this.w.equals(t.w)) {
                return -this.h.compareTo(t.h);
            } else {
                return this.w.compareTo(t.w);
            }
        }
    }

    private List<Envelop> envelops = new ArrayList<>();

    public int maxEnvelopes(int[][] envelopes) {
        for (int[] envelope : envelopes) {
            this.envelops.add(new Envelop(envelope[0], envelope[1]));
        }
        Collections.sort(this.envelops);
        int[] dp = new int[envelopes.length];
        for (int i = 0; i < envelopes.length; i++) {
            dp[i] = 1;
        }
        int maxans = 0;
        for (int i = 0; i < envelopes.length; i++) {
            int longestLower = 0;
            for (int j = 0; j < i; j++) {
                if (this.envelops.get(j).h < this.envelops.get(i).h && dp[j] > longestLower) {
                    longestLower = dp[j];
                }
            }
            dp[i] = longestLower + 1;
            maxans = Math.max(maxans, dp[i]);
        }
        return maxans;
    }
}